
import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;

import java.util.Scanner;

public class Test {
    public static void welcome() {
        System.out.println("----------欢迎进入游戏世界----------\n");
        System.out.println("******************");
        System.out.println("***  猜拳,开始  ***");
        System.out.println("******************\n");
        System.out.println("出拳规则:1.剪刀 2.石头 3.布");
    }

    public static void ready(String pname, int pchoice, Person person, Computer computer) {
        if (pchoice == 1) {
            computer.setName("刘备");
        } else if (pchoice == 2) {
            computer.setName("孙权");
        } else if (pchoice == 3) {
            computer.setName("曹操");
        }
        person.setName(pname);
        System.out.println(person.getName() + " VS " + computer.getName() + "  对战");
    }

    public static void ifStart() {
        Scanner input = new Scanner(System.in);
        while (true) {
            String startAnswer = input.next();
            if (startAnswer.equals("y")) {
                break;
            } else if (startAnswer.equals("n")) {
                System.out.println("退出游戏");
                System.exit(0);
            } else {
                System.out.println("输入非法字符,请重新输入");
            }
        }
    }

    public static void gameResult(String perresult, String comresult, Rule rule, Person person, Computer computer) {
        int resultNum = rule.whoWin(perresult, comresult);
        if (resultNum == 1) {
            System.out.println("哈哈.你输啦!");
        } else if (resultNum == 2) {
            System.out.println("恭喜,你赢了!");
        } else {
            System.out.println("平局");
        }
        rule.scoreChange(resultNum, person, computer);
    }

    public static boolean ifContinue() {
        Scanner input = new Scanner(System.in);

        while (true) {
            System.out.println("是否开启下一轮?(y/n)");
            String ifContinue = input.next();
            if (ifContinue.equals("y")) {
                return true;
            } else if (ifContinue.equals("n")) {
                return false;
            } else {
                System.out.println("输入非法字符,请重新输入");
            }

        }
    }

    public static int gameScore(Person person, Computer computer) {
        System.out.println("姓名      得分");
        System.out.println(person.getName() + "      " + person.getScore());
        System.out.println(computer.getName() + "      " + computer.getScore());
        if (computer.getScore() > person.getScore()) {
            return 1;
        } else if (computer.getScore() < person.getScore()) {
            return 2;
        } else {
            return 0;
        }
    }

    public static String gameend(int a) {
        if (a == 1) {
            return "结果:再接再厉!";
        } else if (a == 2) {
            return "结果:恭喜恭喜!";
        } else {
            return "结果:势均力敌!";
        }
    }

    public static boolean ifNext() {
        Scanner input = new Scanner(System.in);
        while (true) {
            System.out.println("是否开启下一局?(y/n)");
            String ifContinue = input.next();
            if (ifContinue.equals("y")) {
                return true;
            } else if (ifContinue.equals("n")) {
                return false;
            } else {
                System.out.println("输入非法字符,请重新输入");
            }
        }
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        welcome();
        do {
            int pchoice = 0;
            while (true){
                System.out.println("请选择对方角色(1.刘备 2.孙权 3.曹操):");
                pchoice = input.nextInt();
                if (pchoice == 1 || pchoice == 2 || pchoice == 3){
                    break;
                }else {
                    System.out.println("输入非法字符,请重新选择");
                }
            }
            System.out.println("请输入你的姓名:");
            String pname = input.next();

            Person person = new Person();
            Computer computer = new Computer();
            Rule rule = new Rule();

            ready(pname, pchoice, person, computer);
            System.out.println("要开始吗?(y/n)");
            ifStart();
            int time = 0;
            do {
                System.out.println("请出拳:1.剪刀 2.石头 3.布(输入相应数字):");
                int perchoice = input.nextInt();
                int comchoice = computer.getNum();
                String perresult = person.ppunch(perchoice);
                String comresult = computer.cpunch(comchoice);
                System.out.println("你出拳:" + perresult);
                System.out.println(computer.getName() + "出拳:" + comresult);
                gameResult(perresult, comresult, rule, person, computer);
                time++;
            } while (ifContinue());
            System.out.println(computer.getName() + "  VS  " + person.getName());
            System.out.println("对战次数:" + time);
            int a = gameScore(person, computer);
            String end = gameend(a);
            System.out.println(end);
        } while (ifNext());
        System.out.println("游戏结束!");
    }
}
